package com.sqoq.downloader3.ui

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color
import android.net.http.SslError
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.SslErrorHandler
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.sqoq.downloader3.R
import com.sqoq.downloader3.base_ddg.BaseActivity_ddg
import com.sqoq.downloader3.databinding.ActivityLunchDdgBinding
import com.sqoq.downloader3.databinding.ActivityWebDdgBinding

class WebActivity_ddg : BaseActivity_ddg<ActivityWebDdgBinding>() {
    companion object {
        fun start(context: Context, url_ddg: String, title_ddg: String = "") {
            var intent =
                Intent(context, WebActivity_ddg::class.java)
                    .putExtra("title_ddg", title_ddg)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .putExtra("url_ddg", url_ddg)
            context.startActivity(intent)
        }
    }

    override val layout: Int
        get() = R.layout.activity_web_ddg

    override fun initView() {
        val webSettings = binding.webviewDdg.settings
        webSettings.useWideViewPort = true
        webSettings.javaScriptEnabled = true

        binding.titleDdg.setTitleDdg(intent.getStringExtra("title_ddg")!!)
        binding.titleDdg.setThemeColor(Color.BLACK)
        binding.webviewDdg.isVerticalScrollBarEnabled = true
        webSettings.loadWithOverviewMode = true


        binding.webviewDdg.requestFocus()
        binding.webviewDdg.webViewClient = object : WebViewClient() {
            override fun onReceivedSslError(
                view: WebView,
                handler: SslErrorHandler,
                error: SslError
            ) {
                // 默认是handle.cancel()的，即遇到错误即中断
                val builder: AlertDialog.Builder = AlertDialog.Builder(this@WebActivity_ddg)
                builder.setMessage("SSL authentication failed. Do you want to continue accessing?")
                builder.setPositiveButton("continue",
                    DialogInterface.OnClickListener { dialog, which -> handler.proceed() })
                builder.setNegativeButton("cancel",
                    DialogInterface.OnClickListener { dialog, which -> handler.cancel() })
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        }

        binding.webviewDdg.loadUrl(intent.getStringExtra("url_ddg")!!)
    }


}